package com.peas.platform.module.icims.budget.statistics.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.budget.statistics.api.req.SaveDepartmentPerformance;
import com.peas.platform.module.icims.budget.statistics.controller.vo.DepartmentPerformanceKpiVo;
import com.peas.platform.module.icims.budget.statistics.controller.vo.DepartmentPerformancePlanContentVo;
import com.peas.platform.module.icims.budget.statistics.controller.vo.DepartmentPerformancePlanVo;
import com.peas.platform.module.icims.budget.statistics.domain.DepartmentPerformanceDomain;
import com.peas.platform.module.icims.budget.statistics.dto.DepartmentPerformanceDto;
import com.peas.platform.module.icims.budget.statistics.dto.DepartmentPerformanceKpiDto;
import com.peas.platform.module.icims.budget.statistics.dto.DepartmentPerformancePlanContentDto;
import com.peas.platform.module.icims.budget.statistics.dto.DepartmentPerformancePlanDto;
import com.peas.platform.module.icims.budget.statistics.service.DepartmentPerformanceKpiService;
import com.peas.platform.module.icims.budget.statistics.service.DepartmentPerformancePlanContentService;
import com.peas.platform.module.icims.budget.statistics.service.DepartmentPerformancePlanService;
import com.peas.platform.module.icims.budget.statistics.service.DepartmentPerformanceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentPerformanceServiceImpl extends BaseServiceImpl<DepartmentPerformanceDto> implements DepartmentPerformanceService {

    @Resource
    private DepartmentPerformanceDomain departmentPerformanceDomain;
    @Resource
    private DepartmentPerformanceKpiService departmentPerformanceKpiService;
    @Resource
    private DepartmentPerformancePlanService departmentPerformancePlanService;
    @Resource
    private DepartmentPerformancePlanContentService departmentPerformancePlanContentService;

    @Override
    public BaseDomain<DepartmentPerformanceDto, ? extends BasePo> getDomain() {
        return departmentPerformanceDomain;
    }

    private DepartmentPerformanceDto getOneByYearDepartmentPerformanceType(String year, String departmentPerformanceType) {
        Assert.notBlank(year, "年度不能为空");
        Assert.notBlank(departmentPerformanceType, "部门绩效类型不能为空");
        DepartmentPerformanceDto c = new DepartmentPerformanceDto();
        c.setYear(year);
        c.setDepartmentPerformanceType(departmentPerformanceType);
        return this.getOne(c, false);
    }

    @Transactional
    @Override
    public void saveDepartmentPerformance(SaveDepartmentPerformance vo) {
        DepartmentPerformanceDto dto = BeanUtil.copyProperties(vo, DepartmentPerformanceDto.class);
        int result;
        if (dto.getId() != null) {
            result = this.updateById(dto);
        } else {
            DepartmentPerformanceDto bean = this.getOneByYearDepartmentPerformanceType(vo.getYear(), vo.getDepartmentPerformanceType());
            if (bean != null) {
                dto.setId(bean.getId());
                dto.setVersion(bean.getVersion());
                result = this.updateById(dto);
            } else {
                result = this.save(dto);
            }
        }
        Assert.isTrue(result > 0, "保存失败");
        List<DepartmentPerformanceKpiDto> dpkds = departmentPerformanceKpiService.listByField("departmentPerformanceId", dto.getId());
        departmentPerformanceKpiService.removeBatchByList(dpkds);
        List<DepartmentPerformancePlanDto> dppds = departmentPerformancePlanService.listByField("departmentPerformanceId", dto.getId());
        departmentPerformancePlanService.removeBatchByList(dppds);
        List<DepartmentPerformancePlanContentDto> dppcds = departmentPerformancePlanContentService.listByField("departmentPerformanceId", dto.getId());
        departmentPerformancePlanContentService.removeBatchByList(dppcds);
        //kpis
        List<DepartmentPerformanceKpiDto> dpkdList = new ArrayList<>();
        CollUtil.forEach(vo.getKpis(), (CollUtil.Consumer<DepartmentPerformanceKpiVo>) (v, index) -> {
            DepartmentPerformanceKpiDto dpkd = BeanUtil.copyProperties(v, DepartmentPerformanceKpiDto.class);
            dpkd.setDepartmentPerformanceId(dto.getId());
            dpkdList.add(dpkd);
        });
        departmentPerformanceKpiService.saveBatch(dpkdList);
        //plans
        CollUtil.forEach(vo.getPlans(), (CollUtil.Consumer<DepartmentPerformancePlanVo>) (v1, index) -> {
            DepartmentPerformancePlanDto dppd = BeanUtil.copyProperties(v1, DepartmentPerformancePlanDto.class);
            dppd.setDepartmentPerformanceId(dto.getId());
            departmentPerformancePlanService.save(dppd);
            //planContents
            CollUtil.forEach(v1.getPlanContents(), (CollUtil.Consumer<DepartmentPerformancePlanContentVo>) (item, index1) -> {
                DepartmentPerformancePlanContentDto dppcd = BeanUtil.copyProperties(item, DepartmentPerformancePlanContentDto.class);
                dppcd.setDepartmentPerformanceId(dto.getId());
                dppcd.setDepartmentPerformancePlanId(dppd.getId());
                departmentPerformancePlanContentService.save(dppcd);
            });
        });
    }
}
